{% from 'layout/macros.html' import render_pagination with context %}

{% if results.total == 0 %}
<div class="row mt-5">
    <div class="col-lg text-center">
        <div class="alert alert-info">No query logs found</div>
    </div>
</div>
{% else %}
<div class="row mt-5">
    <div class="col-lg">
        <div class="clearfix mb-3">
            <div class="float-right">
                <form action="{{ url_for('logs.export') }}/?{{ params.url() }}" method="post">
                    <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                    <button type="submit" class="btn btn-primary">export to csv</button>
                </form>
            </div>
        </div>

        <table class="table table-sm table-striped table-responsive-lg">
            <thead>
            <tr>
                <th>#</th>
                <th>Domain</th>
                <th style="white-space: nowrap;">Source IP</th>
                <th class="text-right">Type</th>
                <th class="text-center">Forwarded</th>
                <th class="text-center">Matched</th>
                <th class="text-center">Blocked</th>
                <th class="text-right">Date</th>
                <th class="text-right"></th>
            </tr>
            </thead>
            <tbody>
            {% for result in results.items %}
                {% set is_zone = zone_exists(domain=result.domain) %}
                {% set has_alias = (result.source_ip in aliases) %}
            <tr>
                <td>{{ loop.index + (params.per_page * (params.page - 1)) }}</td>
                <td style="width: 100%">
                    {% if is_zone %}
                        <a href="{{ url_for('dns.zone_view', dns_zone_id=result.dns_zone_id) }}">{{ result.domain }}</a>
                    {% else %}
                        {{ result.domain }}
                    {% endif %}
                </td>
                <td title="{{ result.source_ip }}">{{ aliases[result.source_ip] if has_alias else result.source_ip }}</td>
                <td class="text-right">{{ result.type }}</td>
                <td class="text-center">
                    {% set class = 'success' if result.forwarded else 'danger' %}
                    {% set icon = 'check' if result.forwarded else 'times' %}
                    <span class="text-{{ class }}"><i class="fas fa-{{ icon }} fa-fw"></i></span>
                </td>
                <td class="text-center">
                    {% set class = 'success' if result.found else 'danger' %}
                    {% set icon = 'check' if result.found else 'times' %}
                    <span class="text-{{ class }}"><i class="fas fa-{{ icon }} fa-fw"></i></span>
                </td>
                <td class="text-center">
                    {% set class = 'success' if result.blocked else 'danger' %}
                    {% set icon = 'check' if result.blocked else 'times' %}
                    <span class="text-{{ class }}"><i class="fas fa-{{ icon }} fa-fw"></i></span>
                </td>
                <td class="text-right" style="white-space: nowrap;">{{ result.created_at.strftime('%Y-%m-%d %H:%M:%S') }}</td>
                <td class="text-right" style="white-space: nowrap;">
                    <a class="btn btn-secondary btn-sm dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        <i class="fas fa-ellipsis-h"></i>
                    </a>

                    <div class="dropdown-menu" aria-labelledby="dropdownMenuLink">
                        <a href="{{ url_for('logs.index', domain=result.domain) }}" class="dropdown-item"> <i class="fas fa-filter fa-fw"></i> <span class="ml-1">filter by domain</span></a>
                        <a href="{{ url_for('logs.index', source_ip=result.source_ip) }}" class="dropdown-item"> <i class="fas fa-filter fa-fw"></i> <span class="ml-1">filter by IP</span></a>

                        {% if result.dns_zone_id == 0 and is_zone == False %}
                        <form action="{{ url_for('dns.zone_create_from_log', query_log_id=result.id) }}" method="post">
                            <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                            <a href="#" class="submit-on-click dropdown-item"><i class="fas fa-plus-square fa-fw"></i> <span class="ml-1">create zone</span></a>
                        </form>
                        {% else %}
                        <a href="{{ url_for('dns.zone_view', dns_zone_id=result.dns_zone_id) }}" class="dropdown-item"><i class="fas fa-external-link-alt fa-fw"></i> <span class="ml-1">view zone</span></a>
                        {% endif %}

                        {% if not has_alias %}
                        <a href="{{ url_for('dns.alias_edit', alias_id=0, ip=result.source_ip) }}" class="dropdown-item"><i class="fas fa-plus-square fa-fw"></i> <span class="ml-1">create alias</span></a>
                        {% endif %}

                        <form action="{{ url_for('dns.zone_restriction_create_from_log', query_log_id=result.id) }}" method="post">
                            <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                            <a href="{{ url_for('dns.zone_restriction_create_from_log', query_log_id=result.id) }}" class="submit-on-click dropdown-item"><i class="fas fa-ban fa-fw"></i> <span class="ml-1">block IP</span></a>
                        </form>

                        <div class="dropdown-divider"></div>

                        <a href="#" class="dropdown-item toggle-log-response" data-id="{{ result.id }}"><i class="fas fa-glasses fa-fw"></i> <span class="ml-1">view response</span></a>
                    </div>
                </td>
            </tr>
            <tr class="d-none log-details-{{ result.id }}">
                <td></td>
                <td colspan="7">
                    {% set text = (result.data|escape).replace("\n", "<br>"|safe) %}
                    {% if text|length == 0 %}
                        No data
                    {% else %}
                        {{ text }}
                    {% endif %}
                </td>
                <td></td>
            </tr>
            <!-- Leave this to maintain the stripping -->
            <tr class="d-none">
                <td colspan="9"></td>
            </tr>
            {% endfor %}
            </tbody>
        </table>

        {% if results.pages > 1 %}
            {{ render_pagination(results, url_for(page_url), params.url()) }}
        {% endif %}
    </div>
</div>
{% endif %}

<script type="text/javascript">
    $(document).ready(function() {
        $('.toggle-log-response').click(function() {
            var detailRow = $('.log-details-' + $(this).data('id'));
            if (!detailRow) {
                return false;
            }

            if ($(detailRow).hasClass('d-none')) {
                $(detailRow).removeClass('d-none');
            } else {
                $(detailRow).addClass('d-none');
            }
            return false;
        });
    });
</script>